#include "Model3D.h"
#include <assimp/scene.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <QFileInfo>


Model3D::Model3D(QObject *parent)
    : QObject(parent)
    , scene_(nullptr)
{}

Model3D::~Model3D()
{
    if (scene_)
        delete scene_;
}

bool Model3D::loadScene(const QString &filePath, QString& errMsg)
{
    Assimp::Importer importer;
    const aiScene* s = importer.ReadFile(qPrintable(filePath),  aiProcess_Triangulate |
                                                                aiProcess_FlipUVs |
                                                                aiProcess_JoinIdenticalVertices |
                                                                aiProcess_SortByPType);

    // check for errors
    if (!s || s->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !s->mRootNode) {
        errMsg = importer.GetErrorString();
        return false;
    }

    if (scene_)
        delete scene_;

    scene_ = importer.GetOrphanedScene();
    QFileInfo info(filePath);
    modelDir_ = info.absoluteDir();
    emit sceneChanged();
    return true;
}

